<template>
    <div class="wrapper">
        <v-head></v-head>
        <v-sidebar :key="timer"></v-sidebar>
        <span>{{sidebarPage}}</span>
        <div :class="[sidebarPage*1===1?'content-box':'content-box-2',collapse?'content-collapse':'']">
            <v-tags></v-tags>
            <div class="content">
                <transition :name="transitionName" mode="out-in">
                    <keep-alive :include="tagsList">
                        <router-view></router-view>
                    </keep-alive>
                </transition>
                <el-backtop target=".content"></el-backtop>
            </div>
        </div>
    </div>
</template>

<script>
    import vHead from './Header.vue';
    import vSidebar from './Sidebar.vue';
    import vTags from './Tags.vue';
    import bus from './bus';
    import { mapGetters } from 'vuex';

    export default {
        data() {
            return {
                tagsList: [],
                collapse: false,
                timer: '',
                transitionNum: '',
                transitionName: ''
            };
        },
        watch: {
            //检测路由变化改变页面进去动画
            '$route'() {
                this.getRandomInt(1, 6);
            }
        },
        computed: {
            ...mapGetters(['sidebarPage'])
        },
        components: {
            vHead,
            vSidebar,
            vTags
        },
        methods: {
            handleLoad() {
                this.timer = new Date().getTime();
            },
            getRandomInt(min, max) {
                this.transitionNum = Math.floor(Math.random() * (max - min + 1)) + min;
                switch (this.transitionNum) {
                    case 1:
                        this.transitionName = 'el-fade-in-linear';
                        break;
                    case 2:
                        this.transitionName = 'el-fade-in';
                        break;
                    case 3:
                        this.transitionName = 'el-zoom-in-center';
                        break;
                    case 4:
                        this.transitionName = 'el-zoom-in-center';
                        break;
                    case 5:
                        this.transitionName = 'el-zoom-in-top';
                        break;
                    default:
                        this.transitionName = 'el-zoom-in-bottom';
                        break;
                }
            }
        },
        created() {
            bus.$on('collapse-content', msg => {
                this.collapse = msg;
            });

            // 只有在标签页列表里的页面才使用keep-alive，即关闭标签之后就不保存到内存中了。
            bus.$on('tags', msg => {
                let arr = [];
                for (let i = 0, len = msg.length; i < len; i++) {
                    msg[i].name && arr.push(msg[i].name);
                }
                this.tagsList = arr;
            });
            bus.$on('changeRadio', msg => {
                this.handleLoad();
            });
        }
    };
</script>
